package basic.arch.component.lock.aop.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import basic.arch.component.lock.aop.annotation.Lock;
import basic.arch.component.lock.aop.aspect.handle.LockAspectHandle;
/**
 * 锁切面
 * @author jannal
 */
public class LockAspect extends AbstractLockAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private LockAspectHandle lockAspectHandle;

    public Object doAroundMethod(ProceedingJoinPoint pjp, Lock lock) throws Throwable {
        startEnterAspect();
        Object result = null;
        try {
            Object[] argsValue = pjp.getArgs();// 具体的参数值
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            Method method = signature.getMethod();// 方法
            Class<?>[] argsClass = method.getParameterTypes();// 参数类型
            Class<? extends Object> targetClass = pjp.getTarget().getClass();// 目标类
            String targetName = targetClass.getName();
            // 日志打印
            startLog(argsValue, method, argsClass, targetName);

            result = lockAspectHandle.handle(pjp, lock);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        } finally {
            endExitAspect();
        }
    }

    public LockAspectHandle getLockAspectHandle() {
        return lockAspectHandle;
    }

    public void setLockAspectHandle(LockAspectHandle lockAspectHandle) {
        this.lockAspectHandle = lockAspectHandle;
    }

}
